package com.nanhu.test;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import com.nanhu.test.R;
import com.nanhu.test.media.SoundManager;
import com.nanhu.test.sensor.ShakeManager;
import com.nanhu.test.sensor.ShakeManager.OnShakeListener;

public class ImageMatrix extends Activity implements OnTouchListener, OnShakeListener{
	
	private static final int TOTAL_COUNT = 11;
	
	private static final int SWORD_SOUND = 1;
	
	private int count = 1;
	
	private TransformMatrixView view;
	
	private ShakeManager shakeManager;
	
	private SoundManager soundManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		count = 1;
		
		view = new TransformMatrixView(this);
		view.setScaleType(ImageView.ScaleType.MATRIX);
		view.setOnTouchListener(this);
		setContentView(view);
		
		soundManager = new SoundManager();
		soundManager.addSound(SWORD_SOUND, R.raw.sword);
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		shakeManager = new ShakeManager(this);
		shakeManager.setOnShakeListener(this);
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		shakeManager.stop();
	}
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		if(event.getAction() == MotionEvent.ACTION_UP){
			int remainder = count++ % TOTAL_COUNT;
			switch(remainder){
				case 0:
					surfaceRotate();
					break;
				case 1:
					rotate();
					break;
				case 2:
					translate();
					break;
				case 3:
					symmetricalXY();
					break;
				case 4:
					symmetricalV();
					break;
				case 5:
					symmetricalH();
					break;
				case 6:
					shearHV();
					break;
				case 7:
					shearVirtical();
					break;
				case 8:
					shearHorizontal();
					break;
				case 9:
					scale();
					break;
			}
            view.invalidate();
		}
		return true;
	}
	
	
	
	private void printMatrix(Matrix matrix, String tag){
		float[] matrixValues = new float[9];
		matrix.getValues(matrixValues);
		for(int i = 0; i < 3; ++i){
			String temp = new String();
			for(int j = 0; j < 3; ++j){
				temp += matrixValues[3 * i + j ] + " ; ";
			}
			Log.e("TestTransformMatrixActivity 's " + tag, temp);
		}
	}
	
	private void symmetricalXY(){
		Matrix matrix = new Matrix();
		float matrix_values[] = {0f, -1f, 0f, -1f, 0f, 0f, 0f, 0f, 1f};
		matrix.setValues(matrix_values);
		matrix.postTranslate(view.getImageBitmap().getHeight() + view.getImageBitmap().getWidth(),   
              view.getImageBitmap().getHeight() + view.getImageBitmap().getWidth());
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "symmetricalXY");
	}
	
	private void symmetricalV(){
		Matrix matrix = new Matrix();
		float matrix_values[] = {-1f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 1f};
		matrix.setValues(matrix_values);
		matrix.postTranslate(view.getImageBitmap().getWidth() * 2f, 0f);
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "symmetricalV");
	}
	
	private void symmetricalH(){
		Matrix matrix = new Matrix();
		float matrix_values[] = {1f, 0f, 0f, 0f, -1f, 0f, 0f, 0f, 1f};
		matrix.setValues(matrix_values);
		matrix.postTranslate(0f, view.getImageBitmap().getHeight() * 2f);
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "symmetricalH");
	}
	
	private void shearHV(){
		Matrix matrix = new Matrix();
		matrix.setSkew(0.5f, 0.5f);
		matrix.postTranslate(0f, view.getImageBitmap().getHeight()); 
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "shearHV");
	}
	
	private void shearVirtical(){
		Matrix matrix = new Matrix();
		matrix.setSkew(0f, 0.5f);
		matrix.postTranslate(0f, view.getImageBitmap().getHeight());
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "shearVirtical");
	}
	
	private void shearHorizontal(){
		Matrix matrix = new Matrix();
		matrix.setSkew(0.5f, 0f);
		matrix.postTranslate(view.getImageBitmap().getWidth(), 0f);
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "shearHorizontal");
	}
	
	private void scale(){
		Matrix matrix = new Matrix();
		matrix.setScale(2f, 2f); 
		matrix.postTranslate(view.getImageBitmap().getWidth(), view.getImageBitmap().getHeight());
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "scale");
	}
	
	private void surfaceRotate(){
		Matrix matrix = new Matrix();
		matrix.setRotate(45f);
		matrix.preTranslate(-1f * view.getImageBitmap().getWidth() / 2f, -1f * view.getImageBitmap().getHeight() / 2f);
		matrix.postTranslate((float)view.getImageBitmap().getWidth() / 2f, (float)view.getImageBitmap().getHeight() / 2f);
		matrix.postTranslate((float)view.getImageBitmap().getWidth() * 1.5f, 0f);
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "surfaceRotate");
	}
	
	private void rotate(){
		Matrix matrix = new Matrix();
		matrix.setRotate(45f, view.getImageBitmap().getWidth() / 2f, view.getImageBitmap().getHeight());
		matrix.postTranslate(view.getImageBitmap().getWidth() * 1.5f, 0f);
		view.setImageMatrix(matrix);
		
		printMatrix(matrix, "rotate");
	}
	
	private void translate(){
		Matrix matrix = new Matrix();
		// 输出图像的宽度和高度(162 x 251)  
        Log.e("ImageMatrix", "image size: width x height = " +  view.getImageBitmap().getWidth() + " x " + view.getImageBitmap().getHeight());
        matrix.postTranslate(view.getImageBitmap().getWidth(), view.getImageBitmap().getHeight());
        view.setImageMatrix(matrix);
        
        printMatrix(matrix, "translate");
	}
	
	class TransformMatrixView extends ImageView{
		private Bitmap bitmap;	
		private Matrix matrix;
		public TransformMatrixView(Context context){
			super(context);
			bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sophie);
			matrix = new Matrix();
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			canvas.drawBitmap(bitmap, 0, 0, null);
			canvas.drawBitmap(bitmap, matrix, null);
			super.onDraw(canvas);
		}
		
		@Override
		public void setImageMatrix(Matrix matrix) {
			this.matrix.set(matrix);
			super.setImageMatrix(matrix);
		}
		
		public Bitmap getImageBitmap(){
			return bitmap;
		}
	}
	
	@Override
	public void onShake() {
		int remainder = count++ % TOTAL_COUNT;
		switch(remainder){
			case 0:
				surfaceRotate();
				break;
			case 1:
				rotate();
				break;
			case 2:
				translate();
				break;
			case 3:
				symmetricalXY();
				break;
			case 4:
				symmetricalV();
				break;
			case 5:
				symmetricalH();
				break;
			case 6:
				shearHV();
				break;
			case 7:
				shearVirtical();
				break;
			case 8:
				shearHorizontal();
				break;
			case 9:
				scale();
				break;
		}
		soundManager.playSound(SWORD_SOUND);
        view.invalidate();
	}
}
